package org.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
//@DefaultProperties(defaultFallback = "globalHandler") //开启全局降级处理
public class OrderController {
    @Autowired
    OrderService orderService;


    @GetMapping("/consumer/payment/hystrix/{id}")
    public String paymentInfo_OK(@PathVariable("id")Integer id){
        log.info("paymentInfo_OKKKKOKKK");
        return orderService.paymentInfo_OK(id);
    }

    /*//在消费方设置服务降级
    @HystrixCommand(fallbackMethod = "handeException", commandProperties = {
            //设置峰值，超过 1.5 秒，就会调用兜底方法
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })*/
    //@HystrixCommand //不写fallbackMethod属性，默认是全局
    @GetMapping("/consumer/payment/hystrix/timeout/{id}") //服务超时
    public String paymentInfo_Timeout(@PathVariable("id")Integer id){
        log.info("paymentInfo_timeout");
        return orderService.paymentInfo_Timeout(id);
    }


    /*public String handeException(Integer id){
        return "服务调用异常，请稍后再试.....";
    }*/

    //全局服务降级处理在服务消费方上面实现
    // 全局降级处理方法
    /*public String globalHandler(){
        return "这是全局处理降级逻辑的方法.......";
    }*/





    //localhost/consumer/payment/hystrix/1


    //当启动高并发测试时，消费者访问也会变得很慢，甚至出现超时报错
    //localhost/consumer/payment/hystrix/timeout/1

}
